<?php
/*
 *      osmetadictionaryedit.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osbutton.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osedit.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscombobox.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osmetadictionaryfieldedit.php");

class osmetadictionaryedit extends oscomponent {

	private $eduuid;
	private $edname;
	private $cbdictionaries;
	private $dictionary;
	private $metadictionaryfieldedit;
	private $dictionaries;
	private $btadd;
	private $btdel;
	
	function __construct($parent) {
		parent::__construct($parent);
		$this->btadd = new osbutton(0);
		$this->btadd->setevent($this, "onclick", "btaddonclick");
		$this->btadd->caption = "Добавить";
		
		$this->btdel = new osbutton(0);
		$this->btdel->setevent($this, "onclick", "btdelonclick");
		$this->btdel->caption = "Удалить";

		$this->eduuid = new osedit(0);
		$this->eduuid->readonly = true;
		$this->edname = new osedit(0);
		$this->edname->setevent($this, "onchange", "ednameonchange");
		
		$this->cbdictionaries = new oscombobox(0);
		$this->cbdictionaries->setevent($this, "onchange", "cbdictionariesonchange");
		
		$this->metadictionaryfieldedit = new osmetadictionaryfieldedit(0);
	}
	
	private function initialize() {
		if ($this->dictionary) {
			$this->eduuid->value = $this->dictionary->uuid;
			$this->edname->value = $this->dictionary->name;
			$this->eduuid->enable();
			$this->edname->enable();
			$this->metadictionaryfieldedit->fields = $this->dictionary->fields;
		} 
/*
		else {
			$this->eduuid->value = "";
			$this->edname->value = "";
			$this->eduuid->disable();
			$this->edname->disable();
			$this->metadictionaryfieldedit->fields = null;
		}
*/
	}
	
	function cbdictionariesonchange($value) {
		$this->dictionary = $this->dictionaries->byuuid($value);
		$this->initialize();
		$this->update();
	}
	
	function btaddonclick() {
		$this->dictionary = $this->dictionaries->add();
		$this->cbdictionaries->additem($this->dictionary->uuid,$this->dictionary->name);
		$this->initialize();
		$this->cbdictionaries->selectitem($this->dictionary->uuid);
		$this->update();
	}
	
	function btdelonclick() {
		$this->dictionaries->delete($this->cbdictionaries->selecteditem());
		$this->cbdictionaries->deleteitem($this->cbdictionaries->selecteditem());
		$this->dictionary = $this->dictionaries->byuuid($this->cbdictionaries->selecteditem());
		$this->initialize();
		$this->update();
	}
	
	function ednameonchange($value) {
		$this->dictionary->name = $value;
		$this->cbdictionaries->edititem($this->dictionary->uuid, $value);
		$this->cbdictionaries->update();
	}
	
	function __set($name, $value) {
		switch ($name) {
			case "dictionaries":{
				if ($value == null) {
					$this->hide();
				} else {
					$this->dictionaries = $value;
					$this->cbdictionaries->deleteallitems();
					foreach ($this->dictionaries->getall() as $dic) {
						$this->cbdictionaries->additem($dic->uuid,$dic->name);
					}
					$this->dictionary = $this->dictionaries->byuuid($this->cbdictionaries->selecteditem());
					$this->initialize();
					$this->show();
				}
				break;
			}
		}
	}
	
	function get() {
		$ret = "<div id=".$this->uuid;
		$ret .= $this->visible?"":" hidden";
		$ret .= ">";
		$ret .= "<fieldset><legend>Справочники</legend>";
		$ret .= $this->cbdictionaries->get().$this->btadd->get().$this->btdel->get();
		if ($this->dictionary) {
			$ret .= "<fieldset><legend>Свойства</legend>";
			$ret .= $this->eduuid->get()."<br>";
			$ret .= $this->edname->get()."<br>";
			$ret .= "<fieldset><legend>Поля</legend>";
			$ret .= $this->metadictionaryfieldedit->get();
			$ret .= "</fieldset>";
			$ret .= "<fieldset><legend>Таблицы</legend>";
	/*
			$ret .= $this->cbtabulars->get().$this->bttabularadd->get().$this->bttabulardelete->get()."<br></fieldset>";
	*/
			$ret .= "</fieldset>";
		}
		$ret .= "</fieldset></div>";
		return $ret;
	}
}
?>
